var express = require('express');
var router = express.Router();
var dbcomm = require('../util/dbcomm');
// var db = require('../util/db');
var moment = require('moment');
var now = moment().format('YYYY年MM月DD日') ;//日期
var multer  = require('multer');//用express的第三方中间件 multer 实现文件上传功能。
const bodyParser = require('body-parser');
var upload = multer({dest:'./templates/BackStage/image'});
var fs = require('fs')
//处理页面数据 
router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());

// 显示用户的收件箱
router.get('/unreadHint',function(req,res){
    var datas = req.query;
    // console.log(datas);
    // console.log(req.session);
    // req.session.staffName
    // datas.addStaffNo
    var sql = "select * from sys_inbox box left join sys_login staff on staff.staffNo = box.staffNo  where addStaffNo="+req.session.staffNo+" and mailState ="+datas.mailState;
    dbcomm.query(sql,function(err,data){
        if(err) throw err;
        else{
            // console.log(req.session.username);
            // console.log(data);

            res.send(data);
        }
    });
})
//找收件人姓名列表
router.get('/findstaffName',function(req,res){
	dbcomm.query("select staffNo no,staffName name from sys_login",function(err,data){
		if(err) {throw err;}else{
            res.send(data);
		}
	});
})
// 移动邮件
router.get('/takeaway',function(req,res){
    var datas = req.query;
    var sql="";
    dbcomm.query("select mailState from sys_inbox where receiveNo = "+datas.receiveNo,function(err,data){
        if(err)throw err;
        else{
            // console.log(data);
            if(data[0].mailState==0){
                sql="UPDATE sys_inbox SET mailState = 1 where receiveNo = "+datas.receiveNo;
                // console.log(sql);
            }else if(data[0].mailState==1){
                sql="UPDATE sys_inbox SET mailState = 2 where receiveNo = "+datas.receiveNo;
                // console.log(sql);
            }else if(data[0].mailState==2){
                sql="UPDATE sys_inbox SET mailState = 3 where receiveNo = "+datas.receiveNo;
                // console.log(sql);
            }
            dbcomm.query(sql,function(err,data){
            	if(err) {throw err;}else{
                    var result = {};
                    result.result = 'succeed';
                    res.send(result);
            	}
            });
        }
    })
})
// 发送邮件
router.post('/sendmail',upload.single('IMAGE'),function(req,res){
    var datas = req.body;
    var file=req.file;
    var image;
    name=file.originalname;
    nameArray=name.split('');
    var nameMime=[];
    l=nameArray.pop();
    nameMime.unshift(l);
    while(nameArray.length!=0&&l!='.'){
        l=nameArray.pop();
        nameMime.unshift(l);
    }
    Mime=nameMime.join('');
    fs.renameSync('./templates/BackStage/image/'+file.filename,'./templates/BackStage/image/'+file.filename+Mime);
    image = '/BackStage/image/'+file.filename+Mime;
    var myDate = new Date();
    var year=myDate.getFullYear();
    var month=myDate.getMonth()+1;
    var date=myDate.getDate(); 
    var sendTime = year+'/'+month+'/'+date;
    var des = datas.mailContant.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
    var sql = "INSERT INTO `sys_inbox` (`addStaffNo`, `staffNo`, `mailtitle`, `mailContant`, `mailPicture`, `sendTime`) VALUES ("+datas.staffNo+","+req.session.staffNo+" , '"+datas.mailtitle+"', '"+des+"', '"+image+"', '"+sendTime+"')";
    dbcomm.query(sql,function(err,data){
		if(err) {throw err;}else{
            res.redirect('/BackStage/inbox?mailState=0');
		}
	});
})





module.exports = router;